<?php
/**
 *    我的预报系统控制器
 *    @author liangcajian QQ632324010
 */
define('NUM_PER_PAGE', 15);        // 每页显示数量 

class My_kdyubaoApp extends MemberbaseApp
{
  function __construct()
  {
	  $this->My_kdyubaoApp();
  }
  
  function My_kdyubaoApp()
  {
	  parent::__construct();
	  $this->_kd_yubao =& m('kd_yubao');
  }	
    function index()
    {
        /* 搜索条件 */
        $conditions = "baoguo > 0 AND user_id = " . $this->visitor->get('user_id');

		$page = $this->_get_page(NUM_PER_PAGE); 
		$yubaos = $this->_kd_yubao->find(array(
			'conditions'    => $conditions,
			'limit'         => $page['limit'],//获取当前页的数据
			'order'         => "yubao_id desc",
			'count'         => true //允许统计
		)); 

		//print_r("$sort $order");
		$page['item_count'] = $this->_kd_yubao->getCount(); //获取统计的数据
		$this->_format_page($page);  
		$this->assign('page_info', $page); //将分页信息传递给视图，用于形成分页条
		$this->assign('yubaos', $yubaos);	 
       
        $this->assign('curitem', Lang::get('kdyubao_list'));	
		$this->_config_seo('title', Lang::get('kdyubao_list') . ' - ' . Lang::get('member_center'));
        $this->display('my_kdyubao.index.html');
    }

  /**
   *    添加地址
   *
   *    @author liangcajian QQ632324010
   *    @return    void
   */
  function add()
  {
	  if (!IS_POST)
	  {
		  //到货仓库
		  $category_mod =& m('zonghecategory');
		  $category = $category_mod->get_list(31);
		  $this->assign('category', $category);

		  //快递公司
		  $m_kd_kuaidi = &af('kd_kuaidi');
		  $this->assign('kuaidi_data', $m_kd_kuaidi->getAll());
		
		  $this->assign('curitem', Lang::get('add_kdyubao'));	
		  $this->_config_seo('title', Lang::get('add_kdyubao') . ' - ' . Lang::get('member_center'));
		  $this->display('my_kdyubao.form.html');			  
	  }
	  else
	  {
		//检查表单是否已正确填写了
		if ($this->check_form()==true)
		{
			$yundanhao = trim($_POST['yundanhao']);
			$yundanhao = str_replace("\r","",$yundanhao);//textarea换行
			$yundanhao = str_replace("\n",",",$yundanhao);//textarea换行
			$yundanhao = str_replace("/",",",$yundanhao);//斜杠换行
			$yundanhao = str_replace("、",",",$yundanhao);//按钮的斜杠、换行
			$yundanhao = str_replace(" ",",",$yundanhao);//按钮的斜杠、换行
			$yundanhao = rtrim($yundanhao,",");
			$yundanhao_array = explode(',',$yundanhao); //分割成数组
			$count_yundanhao = count($yundanhao_array);
			$yundanhao_array = array_unique($yundanhao_array);//过滤相同的单号			

			$yubao_list = $this->_kd_yubao->find(array(
				'fields'        => 'yubao_alias.yundanhao',
				'conditions'    => db_create_in($yundanhao,'yundanhao','text'),
			));
			$yubao_list && $yubao_implode = implode(',',$yubao_list);
			//print_r($yundanhao_array);die();
			
			$yubaolist = array(); $ok_list = "";$number = 0;
			foreach($yubao_list as $key => $list){ $yubaolist[]=$list['yundanhao']; }			
			foreach( $yundanhao_array as $key => $ydh_list )
			{
				if(array_search($ydh_list, $yubaolist) === false){
					$data = array(
					   'baoguo' => 1,
					   'cangku' => trim($_POST['cangku']),
					   'yundanhao' => $ydh_list,
					   'user_id' => $this->visitor->get('user_id'),
					   'user_name' => $this->visitor->get('user_name'),					   
					   'PostOfficeID' => htmldecode($_POST['PostOfficeID']),
					   'GoodsName' => htmldecode($_POST['GoodsName']),
					   'GoodsCount' => htmldecode($_POST['GoodsCount']),
					   'GoodsWorth' => htmldecode($_POST['GoodsWorth']),
					   'GoodsWeight' => htmldecode($_POST['GoodsWeight']),
					   'notes' => htmldecode($_POST['notes']),		
					   'addtime' => time(),	
					   'updatetime' => time(),		
					);
					$yubao_id = $this->_kd_yubao->add($data);
					if ($this->_kd_yubao->get_error()){
						$this->show_warning($this->_kd_yubao->get_error());
						return;
					}						
					$ok_list .= $ydh_list.',';
					$yubao_ids[] = $yubao_id; 
					$number += 1;
				}else{
					$empty_list .= $ydh_list.',';
				}
			
			}
			$empty_list = rtrim($empty_list,",");
			if ($empty_list) $empty_txt = "包裹运单号：" .$empty_list."已经存在。";
			if(!$ok_list){
				$this->show_warning('添加失败，记录已存在。'.$empty_txt); return;
			}
			$this->write_log($this->visitor->get('user_id'), $this->visitor->get('user_name'), '【手机】添加到货预报,ID:'.implode(',',$yubao_ids), 0);//添加操作日志	

			$this->show_message('共录入'.count($yundanhao_array).'记录，成功添加' . $number .'条件记录。' .$empty_txt,
				'back_list',    'index.php?app=my_kdyubao',
				'continue_add', 'index.php?app=my_kdyubao&amp;act=add'
             );			
		}
	  }
  }

  //检查表单的必填项
  function check_form()
  {  
	  if (!$_POST['cangku']){ $this->show_warning('请选择到货仓库'); return false; }
	  if (!$_POST['yundanhao']){ $this->show_warning('请输入包裹运单号'); return false; }
	  if (!$_POST['PostOfficeID']){ $this->show_warning('请选择派件快递公司'); return false; }
	  if (!$_POST['GoodsName']){ $this->show_warning('请输入物品名称'); return false; }
	  if (!$_POST['GoodsCount']){ $this->show_warning('请输入物品数量'); return false; }
	  if (!$_POST['GoodsWorth']){ $this->show_warning('请输入物品价值'); return false; }
	  if (!$_POST['GoodsWeight']){ $this->show_warning('请输入物品重量'); return false; }
  
	  return true;
  }  
  
  
  //删除数据
  function drop()
  {
	  $id = isset($_GET['id']) ? htmldecode($_GET['id']) : '';
	  if (!$id)
	  {
		  $this->show_warning('请选择需要删除的记录');
		  return;
	  }
	  $this->_kd_yubao->drop("user_id = " .$this->visitor->get('user_id') . ' AND status = 0 AND ' . db_create_in($id,'yubao_id'));
	  if ($this->_kd_yubao->has_error())
	  {
		  $this->show_warning($this->_kd_yubao->get_error());
		  return;
	  }
	  $this->write_log($this->visitor->get('user_id'), $this->visitor->get('user_name'), '【手机】删除到货预报,ID:'.$id, 0);//添加操作日志	

	  $this->show_message('drop_ok');	
  }  
  /**
   *    三级菜单
   *
   *    @author liangcajian QQ632324010
   *    @return    void
   */
  function _get_member_submenu()
  {
	  $menus = array(
		  array(
			  'name'  => 'kdyubao_list',
			  'url'   => 'index.php?app=my_kdyubao',
		  ),
		  array(
			  'name'  => 'add_kdyubao',
			  'url'   => 'index.php?app=my_kdyubao&act=add',
		  ),
	  );
	if (ACT == 'edit')
	  {
		  $menus[] = array(
			  'name' => 'edit_kdyubao',
			  'url'  => '',
		  );
	  }
	  return $menus;
  }
  
}
?>